 /*
  * Copyright (c) 2021, 2023, yishun.cn All rights reserved.
  *
  */
 package cn.shun.config;

 import org.springframework.web.WebApplicationInitializer;
 import org.springframework.web.context.ContextLoaderListener;
 import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
 import org.springframework.web.filter.CharacterEncodingFilter;
 import org.springframework.web.servlet.DispatcherServlet;

 import javax.servlet.DispatcherType;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.util.EnumSet;

 /**
  * <p>Project: MyBatis2023 - WebInit
  * <p>Powered by yishun On 2023-02-03 10:49:08
  *
  * @author yishun [2465734101@qq.com]
  * @version 1.0
  * @since 17
  */
 public class WebInit implements WebApplicationInitializer {
     @Override
     public void onStartup(ServletContext servletContext) throws ServletException {
         // Create the 'root' Spring application context             创建基于注解配置的Spring application context web上下文对象
         var rootContext = new AnnotationConfigWebApplicationContext();
         //(1)引入的spring IoC AppConfig.java 相当于 applicationContext.xml     在web上下文中添加ioc容器
         rootContext.register(SpringConfig.class);
         // Manage the lifecycle of the root application context             在servletContext添加监听器，管理rootContext的生命周期
         servletContext.addListener(new ContextLoaderListener(rootContext));
         // Create the dispatcher servlet's Spring application context             添加dispatcher Spring application context web
         var dispatcherContext = new AnnotationConfigWebApplicationContext();
         //(2)引入的springmvc WebInit.java 相当于 springmvc.xml                引入springmvc配置
         dispatcherContext.register(SpringMvcConfig.class);
         // Register and map the dispatcher servlet           在servlet容器中添加dispatcher Spring
         var dispatcher = servletContext.addServlet("dispatcher", new
                 DispatcherServlet(dispatcherContext) {
                     @Override
                     protected void noHandlerFound(HttpServletRequest req, HttpServletResponse resp) throws Exception {
                         //resp.sendRedirect("/static/404.html");
                         req.getRequestDispatcher("/static/404.html").forward(req, resp);
                     }
                 });
         dispatcher.setLoadOnStartup(1);
         dispatcher.addMapping("/");
         //(3) spring 框架默认的编码过滤器 WebFilter 主要解决post提交，和PrintWriter 输出乱码问题
         var csf = new CharacterEncodingFilter("utf-8");
         csf.setEncoding("utf-8");
         csf.setForceEncoding(true);
         csf.setForceResponseEncoding(true);
         csf.setForceResponseEncoding(true);
         var cf = servletContext.addFilter("cs", csf);
         //springmvc拦截所有请求与响应
         cf.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), true, "/*");
     }
 }